package eworldpacs.ui;

import leadtools.ILeadStream;
import leadtools.LeadSizeF;
import leadtools.LeadStreamFactory;
import leadtools.RasterImage;
import leadtools.RasterImageFormat;
import leadtools.codecs.RasterCodecs;
import leadtools.controls.ImageViewerCenterAtInteractiveMode;
import leadtools.controls.ImageViewerInteractiveMode;
import leadtools.controls.ImageViewerNoneInteractiveMode;
import leadtools.controls.ImageViewerSizeMode;
import leadtools.controls.ImageViewerMagnifyGlassInteractiveMode;
import leadtools.controls.ImageViewerPanZoomInteractiveMode;
import leadtools.controls.RasterImageViewer;
import leadtools.demos.Messager;
import leadtools.demos.OpenFileDialog;
import leadtools.demos.Progress;
import leadtools.demos.SplashScreen;
import leadtools.demos.Support;
import leadtools.demos.Utils;
import leadtools.dicom.DicomDataSet;
import leadtools.dicom.DicomDataSetLoadFlags;
import leadtools.dicom.DicomElement;
import leadtools.dicom.DicomEngine;
import leadtools.dicom.DicomGetImageFlags;
import leadtools.dicom.DicomLoadAsyncCompletedListener;
import leadtools.dicom.DicomTag;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.Color;
import android.graphics.PointF;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URI;
import java.net.URISyntaxException;

import com.eworld.pacsviewer.R;

public class DatasetDemoActivity extends Activity {

	private static final String DEMO_STARTED = "demo_started";
	private static final String IS_PLAYING = "is_playing";
	private DicomDataSet _loadedDataSet;
	private RasterImageViewer _imageViewer;
	private TextView _noImageTextView;
	private RasterImage _loadedImage;
	private SeekBar _frameSeekBar;
	private Button _playButton;
	private Button _stopButton;
	private EditText _currentPageEditText;
	private View _moreOptionsLinearLayout;
	private TextView _selectedUrlTextView;
	private ImageViewerSizeMode _currentSizeMode;
	private ImageViewerInteractiveMode _currentInteractiveMode = null;
	private WindowLeveler _windowLeveler = null;
	private Handler _playbackHandler;

	boolean _processingOptionClick = false;
	private Button _resetWindowLevelButton;
	private ProgressDialog mProgressDlg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Support.setLicense(this);
		if (Support.isKernelExpired()) {
			Messager.showKernelExpiredMessage(this, new OnDismissListener() {
				@Override
				public void onDismiss(DialogInterface dialog) {
					finish();
				}
			});

			return;
		}

		DicomEngine.startup();
		boolean demoStarted = false;
		boolean isPlaying = false;
		if (savedInstanceState != null) {
			demoStarted = savedInstanceState.getBoolean(DEMO_STARTED);
			isPlaying = savedInstanceState.getBoolean(IS_PLAYING);
		}

		if (!demoStarted) {

			DicomEngine.startup();
			if (Support.isKernelExpired()) {
				Messager.showKernelExpiredMessage(this,
						new OnDismissListener() {
							@Override
							public void onDismiss(DialogInterface dialog) {
								finish();
							}
						});
				return;
			}
		}

		_loadedDataSet = ((DicomDemoApplication) getApplicationContext())
				.getLoadedDataSet();
		if (_loadedDataSet != null) {
			_loadedImage = ((DicomDemoApplication) getApplicationContext())
					.getLoadedImage().cloneAll();
			((DicomDemoApplication) getApplicationContext())
					.setLoadedImage(null);
			((DicomDemoApplication) getApplicationContext())
					.setLoadedImage(_loadedImage);
			_windowLeveler = ((DicomDemoApplication) getApplicationContext())
					.getWindowLeveler();
			_currentInteractiveMode = ((DicomDemoApplication) getApplicationContext())
					.getLastInteractiveMode();
		}

		_imageViewer = (RasterImageViewer) findViewById(R.id.imageviewer);
		_imageViewer.setImage(_loadedImage);
		_resetWindowLevelButton = (Button) findViewById(R.id.resetWindowLevelButton);
		_noImageTextView = (TextView) findViewById(R.id.noImageTextView);
		_frameSeekBar = (SeekBar) findViewById(R.id.frameSeekBar);
		_playButton = (Button) findViewById(R.id.playButton);
		_stopButton = (Button) findViewById(R.id.stopButton);
		_currentPageEditText = (EditText) findViewById(R.id.currentPageEditText);
		_moreOptionsLinearLayout = (View) findViewById(R.id.moreOptionsLinearLayout);

		_currentSizeMode = ImageViewerSizeMode.FIT_ALWAYS;
		_imageViewer.setSizeMode(_currentSizeMode);
		if (_currentInteractiveMode != null)
			_imageViewer.setTouchInteractiveMode(_currentInteractiveMode);
		else if (_windowLeveler != null) {
			_windowLeveler.setOwnerActivity(this);
			_windowLeveler.setImage(_loadedImage);
			_windowLeveler.onActivityRecreated();
			_imageViewer.setOnTouchListener(_windowLeveler);
		}

		if (_windowLeveler == null)
			_resetWindowLevelButton.setEnabled(false);
		else {
			_resetWindowLevelButton.setEnabled(_windowLeveler.canWindowLevel());
		}

		_resetWindowLevelButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (_windowLeveler != null) {
					_windowLeveler.resetLUT();
					_windowLeveler.refresh();
				}

			}
		});

		_frameSeekBar.setMax(0);
		_frameSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				if (_loadedImage != null) {
					_loadedImage.setPage(progress + 1);
					updateCurrentPageEditText();
					if (_windowLeveler != null)
						_windowLeveler.refresh();
				}
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});
		_stopButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (_playbackHandler != null) {
					_playbackHandler = null;
					updatePageInformation(true);
				}
			}

		});

		_playButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				playImageFrames();
			}

		});
		if (!(_loadedDataSet == null)) {

		}
		updatePageInformation(false);
		if (isPlaying)
			playImageFrames();
	}

	private void playImageFrames() {
		if (_playbackHandler == null) {
			_playbackHandler = new Handler();
			Runnable playbackRunnable = new Runnable() {
				public void run() {
					if (_playbackHandler != null) {
						if (_frameSeekBar.getProgress() == _frameSeekBar
								.getMax())
							_frameSeekBar.setProgress(0);
						else {
							if (!_frameSeekBar.isPressed()) {
								_frameSeekBar.incrementProgressBy(1);
							}
						}
						_playbackHandler.postDelayed(this,
								(long) (1000.0f / 60.0f));
					}
				}
			};
			_playbackHandler.postDelayed(playbackRunnable,
					(long) (1000.0f / 60f));

		}
	}

	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {
		// Save the user's current game state
		savedInstanceState.putBoolean(DEMO_STARTED, true);
		savedInstanceState.putBoolean(IS_PLAYING, _playbackHandler != null);
		if (_currentInteractiveMode != null)
			_currentInteractiveMode.stop(_imageViewer);
		if (_windowLeveler != null) {
			_imageViewer.setOnTouchListener(null);
			_imageViewer.setTouchInteractiveMode(null);
			_windowLeveler.setOwnerActivity(null);
		}

		// Always call the superclass so it can save the view hierarchy state
		super.onSaveInstanceState(savedInstanceState);
	}

	@Override
	protected void onResume() {
		super.onResume();
		_imageViewer = (RasterImageViewer) findViewById(R.id.imageviewer);
		_imageViewer.setSizeMode(_currentSizeMode);
		_loadedDataSet = ((DicomDemoApplication) getApplicationContext())
				.getLoadedDataSet();
		if (_currentInteractiveMode != null) {
			_imageViewer.setTouchInteractiveMode(_currentInteractiveMode);
		}
		if (_windowLeveler != null) {
			_windowLeveler.setOwnerActivity(this);
			_windowLeveler.setImage(_imageViewer.getImage());
			_windowLeveler.onActivityRecreated();
			if (_currentInteractiveMode == null)
				_imageViewer.setOnTouchListener(_windowLeveler);
		}
	}

	private void updatePageInformation(boolean resetSeekbar) {
		if (_loadedImage != null) {
			_frameSeekBar.setMax(_loadedImage.getPageCount() - 1);
			if (resetSeekbar)
				_frameSeekBar.setProgress(0);
			if (_loadedImage.getPageCount() > 1) {
				_stopButton.setEnabled(true);
				_playButton.setEnabled(true);
				_frameSeekBar.setEnabled(true);
			} else {
				_stopButton.setEnabled(false);
				_playButton.setEnabled(false);
				_frameSeekBar.setEnabled(false);
			}
		} else {
			_frameSeekBar.setProgress(0);
			_frameSeekBar.setMax(0);
			_stopButton.setEnabled(false);
			_playButton.setEnabled(false);
			_frameSeekBar.setEnabled(false);
		}
		updateCurrentPageEditText();
	}

	private void updateCurrentPageEditText() {
		String currentPageTextFormat = "%d of %d";
		if (_loadedImage != null) {
			String currentPageText = String.format(currentPageTextFormat,
					_loadedImage.getPage(), _loadedImage.getPageCount());
			_currentPageEditText.setText(currentPageText);
		} else {
			_currentPageEditText.setText("0 of 0");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public void onClickMoreButton(View view) {
		Button moreButton = (Button) view;

		if (_moreOptionsLinearLayout.getVisibility() == View.GONE) {
			moreButton.setText("Less...");
			_moreOptionsLinearLayout.setVisibility(View.VISIBLE);
		} else {
			moreButton.setText("More...");
			_moreOptionsLinearLayout.setVisibility(View.GONE);
		}
	}

	public void onClickFileOption(View view) {
		view.setEnabled(false);

		LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View fileOptionsView = layoutInflater.inflate(
				R.layout.file_options, null);

		AlertDialog.Builder fileOptionsDlgBuilder = new AlertDialog.Builder(
				this);
		fileOptionsDlgBuilder.setView(fileOptionsView);
		final AlertDialog fileOptionsDlg = fileOptionsDlgBuilder.create();
		fileOptionsDlg.setTitle("File Options");
		fileOptionsDlg.setButton(AlertDialog.BUTTON_POSITIVE, "Close",
				new Dialog.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						fileOptionsDlg.dismiss();
					}
				});

		RadioGroup fileOptionsRadioGroup = (RadioGroup) fileOptionsView
				.findViewById(R.id.fileOptionsRadioGroup);
		fileOptionsRadioGroup
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						RadioButton loadFileRadioButton = (RadioButton) fileOptionsView
								.findViewById(R.id.loadFileRadioButton);
						RadioButton loadUrlRadioButton = (RadioButton) fileOptionsView
								.findViewById(R.id.loadUrlRadioButton);
						RadioButton saveRadioButton = (RadioButton) fileOptionsView
								.findViewById(R.id.saveRadioButton);
						View loadFileGroup = fileOptionsView
								.findViewById(R.id.loadFileGroup);
						View loadUrlGroup = fileOptionsView
								.findViewById(R.id.loadUrlGroup);
						View saveGroup = fileOptionsView
								.findViewById(R.id.saveGroup);
						enableDisableView(loadFileGroup,
								loadFileRadioButton.isChecked());
						enableDisableView(loadUrlGroup,
								loadUrlRadioButton.isChecked());
						enableDisableView(saveGroup,
								saveRadioButton.isChecked());
					}
				});

		Button loadFileButton = (Button) fileOptionsView
				.findViewById(R.id.loadFileButton);
		loadFileButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				OpenFileDialog.OnFileSelectedListener onFileSelectedListener = new OpenFileDialog.OnFileSelectedListener() {
					@Override
					public void onFileSelected(String fileName) {
						FileInputStream fileInputStream = null;
						try {
							fileInputStream = new FileInputStream(fileName);
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						}
						if (fileInputStream != null) {
							ILeadStream leadStream = LeadStreamFactory.create(
									fileInputStream, true);
							if (leadStream != null) {
								fileOptionsDlg.dismiss();
								mProgressDlg = Progress
										.show(DatasetDemoActivity.this, "",
												"Loading");
								DatasetDemoActivity.this
										.loadDataSetFromStream(leadStream);
							}
						}
					}
				};

				OpenFileDialog openDlg = new OpenFileDialog(
						DatasetDemoActivity.this,
						Utils.getFileFilter(new String[] { ".dcm", ".pre" }),
						onFileSelectedListener);
				openDlg.show();
			}
		});

		Button loadUrlButton = (Button) fileOptionsView
				.findViewById(R.id.loadUrlButton);
		loadUrlButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				if (_selectedUrlTextView != null) {

					AsyncTask<String, Integer, ILeadStream> loadUrlTask = new AsyncTask<String, Integer, ILeadStream>() {

						@Override
						protected void onPreExecute() {
							fileOptionsDlg.dismiss();
							mProgressDlg = Progress.show(
									DatasetDemoActivity.this, "", "Loading");
						}

						@Override
						protected ILeadStream doInBackground(String... inputURL) {
							try {
								URI uri = new URI(inputURL[0]);
								return LeadStreamFactory.create(uri);

								// return true;
							} catch (URISyntaxException e) {
								Context context = getApplicationContext();
								CharSequence text = "Error loading file from URL";
								Toast toast = Toast.makeText(context, text,
										Toast.LENGTH_SHORT);
								toast.show();
								return null;
							}
						}

						@Override
						protected void onPostExecute(ILeadStream leadStream) {
							if (leadStream != null)
								DatasetDemoActivity.this
										.loadDataSetFromStream(leadStream);
						}
					};
					loadUrlTask.execute((String) _selectedUrlTextView.getText());

				}
			}
		});

		Button saveButton = (Button) fileOptionsView
				.findViewById(R.id.saveButton);
		final String sharedLibsPath = Utils.getSharedLibsPath(this);
		saveButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				try {
					EditText saveFileNameEditText = (EditText) fileOptionsView
							.findViewById(R.id.saveFileNameEditText);
					String fileName = saveFileNameEditText.getText().toString();
					RasterCodecs codecs = new RasterCodecs(sharedLibsPath);
					File picturesDirectory = Environment
							.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
					FileOutputStream outputStream = new FileOutputStream(
							picturesDirectory + "/" + fileName, false);
					ILeadStream leadStream = LeadStreamFactory.create(
							outputStream, true);
					codecs.save(_imageViewer.getImage(), leadStream,
							RasterImageFormat.JPEG, 24);
					fileOptionsDlg.dismiss();
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});

		fileOptionsDlg
				.setOnDismissListener(new DialogInterface.OnDismissListener() {
					@Override
					public void onDismiss(final DialogInterface dialog) {
						_selectedUrlTextView = null;
					}
				});
		fileOptionsDlg.getWindow().setWindowAnimations(R.style.DialogAnimation);
		fileOptionsDlg.getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		fileOptionsDlg.show();
		view.setEnabled(true);
	}

	private void loadDataSetFromStream(ILeadStream stream) {
		// String tempPath = "/data/data/eworldpacs/";
		String tempPath = this.getFilesDir().getAbsolutePath();
		final DicomDataSet ds = new DicomDataSet(tempPath);
		ds.addLoadAsyncCompletedListener(new DicomLoadAsyncCompletedListener() {
			@Override
			public void onLoadAsyncCompleted() {
				((DicomDemoApplication) getApplicationContext())
						.setLoadedDataSet(ds);
				_loadedDataSet = ds;
				DicomElement pixelDataElement = _loadedDataSet
						.findFirstElement(null, DicomTag.PIXEL_DATA, true);

				if (pixelDataElement != null) {
					int bitmapCount = ds.getImageCount(pixelDataElement);
					_noImageTextView.setVisibility(View.GONE);
					_loadedImage = getDataSetImages(_loadedDataSet,
							pixelDataElement, bitmapCount);
					((DicomDemoApplication) getApplicationContext())
							.setLoadedImage(_loadedImage);
					updatePageInformation(true);
					_imageViewer.setImage(_loadedImage);
					_imageViewer.setSizeMode(_currentSizeMode);
					if (_playbackHandler != null)
						_playbackHandler = null;
					_playbackHandler = null;

				
					
					_windowLeveler = new WindowLeveler(_loadedImage,
							_loadedDataSet, DatasetDemoActivity.this) {

						EditText _widthEditText = null;
						EditText _centerEditText = null;

						@Override
						protected void onWindowLevelChanged(int width,
								int center) {
							if (_widthEditText == null)
								_widthEditText = (EditText) this
										.getOwnerActivity().findViewById(
												R.id.windowWidthEditText);
							if (_centerEditText == null)
								_centerEditText = (EditText) this
										.getOwnerActivity().findViewById(
												R.id.windowCenterEditText);
							_widthEditText.setText(Integer.toString(width));
							_centerEditText.setText(Integer.toString(center));

						}

						@Override
						public void onActivityRecreated() {
							_widthEditText = null;
							_centerEditText = null;
						}
					};

					_resetWindowLevelButton.setEnabled(_windowLeveler
							.canWindowLevel());

					((DicomDemoApplication) getApplicationContext())
							.setWindowLeveler(_windowLeveler);
					_currentInteractiveMode = ((DicomDemoApplication) getApplicationContext())
							.getLastInteractiveMode();
					if (_currentInteractiveMode == null)
						_imageViewer.setOnTouchListener(_windowLeveler);

				} else {
					_windowLeveler = null;
					_loadedImage = null;
					((DicomDemoApplication) getApplicationContext())
							.setLoadedImage(null);
					_imageViewer.setImage(null);
					_imageViewer.setOnTouchListener(null);
					updatePageInformation(true);
					_resetWindowLevelButton.setEnabled(false);
					_noImageTextView.setVisibility(View.VISIBLE);
				}

				Progress.close(mProgressDlg);
			}

		});
		ds.loadAsync(stream, DicomDataSetLoadFlags.LOAD_AND_CLOSE);

	}

	public RasterImage getDataSetImages(DicomDataSet ds, DicomElement element,
			int imageCount) {
		RasterImage resultImage = null;
		for (int x = 0; x < imageCount; x++) {
			RasterImage image = null;

			try {
				image = _loadedDataSet.getImage(element, x, 0,
						DicomGetImageFlags.AUTO_APPLY_MODALITY_LUT
								| DicomGetImageFlags.AUTO_APPLY_VOI_LUT);
			} catch (Exception ex) {
				// DemoUtilities.ShowToast(ex);
			}

			if (image != null) {
				if (x == 0) {
					resultImage = image;
				} else {
					resultImage.addPage(image);
				}
			} else {
				// string errorMsg =
				// string.Format("Note that this dataset contains an invalid image.  No image will be displayed ");
				// DemoUtilities.ShowToast(errorMsg);
				break;
			}
		}
		return resultImage;
	}

	public void onClickImageUrl(View view) {
		TextView urlTextView = (TextView) view;
		if (_selectedUrlTextView != urlTextView) {
			if (_selectedUrlTextView != null) {
				_selectedUrlTextView.setTextColor(Color.WHITE);
			}
			urlTextView.setTextColor(Color.RED);
			_selectedUrlTextView = urlTextView;
		}
	}

	private void enableDisableView(View view, boolean enabled) {
		view.setEnabled(enabled);
		view.setVisibility(enabled ? View.VISIBLE : View.GONE);
		if (view instanceof TextView) {
			TextView textView = (TextView) view;
			if (textView != _selectedUrlTextView) {
				textView.setTextColor(enabled ? Color.WHITE : Color.rgb(139,
						139, 139));
			} else {
				textView.setTextColor(enabled ? Color.RED : Color
						.rgb(139, 0, 0));
			}
		}

		if (view instanceof EditText) {
			EditText editText = (EditText) view;
			editText.setTextColor(enabled ? Color.BLACK : Color.rgb(139, 139,
					139));
		}

		if (view instanceof ViewGroup) {
			ViewGroup group = (ViewGroup) view;
			for (int idx = 0; idx < group.getChildCount(); idx++) {
				enableDisableView(group.getChildAt(idx), enabled);
			}
		}
	}

	public void onClickViewOption(View view) {
		view.setEnabled(false);

		LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View viewOptionsView = layoutInflater.inflate(
				R.layout.view_options, null);
		final RadioGroup sizeModeRadioGroup = (RadioGroup) viewOptionsView
				.findViewById(R.id.sizeModeRadioGroup);
		final RadioGroup interactiveModeRadioGroup = (RadioGroup) viewOptionsView
				.findViewById(R.id.interactiveModeRadioGroup);

		AlertDialog.Builder viewOptionsDlgBuilder = new AlertDialog.Builder(
				this);
		viewOptionsDlgBuilder.setView(viewOptionsView);
		final AlertDialog viewOptionsDlg = viewOptionsDlgBuilder.create();
		viewOptionsDlg.setTitle("View Options");
		viewOptionsDlg.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
				new Dialog.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						ImageViewerSizeMode sizeMode = null;
						ImageViewerInteractiveMode interactiveMode = null;

						switch (sizeModeRadioGroup.getCheckedRadioButtonId()) {
						case R.id.normalRadioButton:
							sizeMode = ImageViewerSizeMode.ACTUAL_SIZE;
							break;
						case R.id.fitRadioButton:
							sizeMode = ImageViewerSizeMode.FIT;
							break;
						case R.id.fitAlwaysRadioButton:
							sizeMode = ImageViewerSizeMode.FIT_ALWAYS;
							break;
						case R.id.fitWidthRadioButton:
							sizeMode = ImageViewerSizeMode.FIT_WIDTH;
							break;
						}

						switch (interactiveModeRadioGroup
								.getCheckedRadioButtonId()) {
						case R.id.windowLevelRadioButton:
							((DicomDemoApplication) getApplicationContext())
									.setLastInteractiveMode(null);
							_currentInteractiveMode = null;
							_imageViewer.setOnTouchListener(_windowLeveler);
							break;
						case R.id.centerAtRadioButton:
							interactiveMode = new ImageViewerCenterAtInteractiveMode();
							break;
						case R.id.magGlassRadioButton:
							ImageViewerMagnifyGlassInteractiveMode magGlassInteractiveMode = new ImageViewerMagnifyGlassInteractiveMode();
							LeadSizeF magGlassSize = magGlassInteractiveMode
									.getSize();
							PointF offset = new PointF(
									magGlassSize.getWidth() / 2, magGlassSize
											.getHeight() / 2);
							magGlassInteractiveMode.setOffset(offset);
							interactiveMode = magGlassInteractiveMode;
							break;
						case R.id.panZoomRadioButton:
							interactiveMode = new ImageViewerPanZoomInteractiveMode();
							break;
						case R.id.noneRadioButton:
							interactiveMode = new ImageViewerNoneInteractiveMode();
							break;
						}

						if (sizeMode != null) {
							_currentSizeMode = sizeMode;
							_imageViewer.setScaleFactor(1.0f);
							_imageViewer.setSizeMode(_currentSizeMode);
						}
						if (interactiveMode != null) {
							((DicomDemoApplication) getApplicationContext())
									.setLastInteractiveMode(interactiveMode);
							_currentInteractiveMode = interactiveMode;
							_imageViewer.setOnTouchListener(null);
							_imageViewer
									.setTouchInteractiveMode(_currentInteractiveMode);
						}
						viewOptionsDlg.dismiss();
					}
				});

		RadioButton currentSizeModeRadioButton = null;
		switch (_currentSizeMode) {
		case ACTUAL_SIZE:
			currentSizeModeRadioButton = (RadioButton) viewOptionsView
					.findViewById(R.id.normalRadioButton);
			break;
		case FIT:
			currentSizeModeRadioButton = (RadioButton) viewOptionsView
					.findViewById(R.id.fitRadioButton);
			break;
		case FIT_ALWAYS:
			currentSizeModeRadioButton = (RadioButton) viewOptionsView
					.findViewById(R.id.fitAlwaysRadioButton);
			break;
		case FIT_WIDTH:
			currentSizeModeRadioButton = (RadioButton) viewOptionsView
					.findViewById(R.id.fitWidthRadioButton);
			break;
		default:
			break;
		}
		if (currentSizeModeRadioButton != null)
			sizeModeRadioGroup.check(currentSizeModeRadioButton.getId());

		RadioButton currentInteractiveModeRadioButton = null;

		if (_currentInteractiveMode == null) {
			currentInteractiveModeRadioButton = (RadioButton) viewOptionsView
					.findViewById(R.id.windowLevelRadioButton);
		} else if (_currentInteractiveMode instanceof ImageViewerCenterAtInteractiveMode) {
			currentInteractiveModeRadioButton = (RadioButton) viewOptionsView
					.findViewById(R.id.centerAtRadioButton);
		} else if (_currentInteractiveMode instanceof ImageViewerMagnifyGlassInteractiveMode) {
			currentInteractiveModeRadioButton = (RadioButton) viewOptionsView
					.findViewById(R.id.magGlassRadioButton);
		} else if (_currentInteractiveMode instanceof ImageViewerPanZoomInteractiveMode) {
			currentInteractiveModeRadioButton = (RadioButton) viewOptionsView
					.findViewById(R.id.panZoomRadioButton);
		} else if (_currentInteractiveMode instanceof ImageViewerNoneInteractiveMode) {
			currentInteractiveModeRadioButton = (RadioButton) viewOptionsView
					.findViewById(R.id.noneRadioButton);
		}

		if (currentInteractiveModeRadioButton != null)
			interactiveModeRadioGroup.check(currentInteractiveModeRadioButton
					.getId());

		viewOptionsDlg.getWindow().setWindowAnimations(R.style.DialogAnimation);
		viewOptionsDlg.show();
		view.setEnabled(true);
	}

	public void onClickViewDicomElementsOption(View view) {
		if (_loadedDataSet == null)
			return;

		Intent intent = new Intent();
		intent.setClass(this, ElementViewActivity.class);
		this.startActivity(intent);
	}

	public void onClickViewDicomTables(View view) {
		Intent intent = new Intent();
		intent.setClass(this, TablesViewActivity.class);
		this.startActivity(intent);
	}
}